package com.example.xiangmuservice.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.api.Result;
import com.example.api.dto.WwwDTO;
import com.example.api.service.WwwService;
import com.example.api.utils.HttpStatusEnum;
import com.example.api.vo.WwwVO;
import com.example.xiangmuservice.dao.WwwRepository;
import com.example.xiangmuservice.entity.Www;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Slf4j
@Service
public class WwwServiceImpl implements WwwService {

    @Autowired
    WwwRepository wwwRepository;


    @Override
    public String findById(Integer id) {
        Optional<Www> optional = wwwRepository.findById(Long.valueOf(id));
        if (optional.isPresent()) {
            return JSON.toJSONString(optional.get());
        }
        return null;
    }

    @Override
    public Result insert(WwwVO wwwVO) {
        Long id = wwwVO.getId();
        log.info("配置文件插入数据wwwVO:{}", JSONObject.toJSONString(wwwVO));
//        BeanCopier copier = BeanCopier.create(WwwVO.class,Www.class,false);
        Www www = new Www();
//        copier.copy(wwwVO,www,null);
        www.setId(wwwVO.getId());
        www.setAddress(wwwVO.getAddress());
        www.setAge(wwwVO.getAge());
        www.setName(wwwVO.getName());
        wwwRepository.save(www);
        return Result.success(HttpStatusEnum.OPERATION_SUCCESS, HttpStatusEnum.OPERATION_SUCCESS.reasonPhraseCN());
    }

    @Override
    @Transactional
    public Long save(WwwDTO wwwDTO) {
        List<Www> wwwList = new ArrayList<>();
        for (int i = 0; i<20;i++){
            Www www = new Www();
            www.setId(wwwDTO.getId());
            www.setAge(wwwDTO.getAge());
            www.setAddress(wwwDTO.getAddress());
            www.setName(wwwDTO.getName());
            wwwList.add(www);
//            wwwRepository.save(www);
            }
        List<Www> s = wwwRepository.saveAll(wwwList);
        return null;

    }



}
